import {
  getAreaList, getActionList, createNum, addDevice, getCameraInfo, deleteCamera, updateDevice,
  getCameraAreaList, addArea, updateArea, deleteArea, getAccidentList
} from '@/api/innerProject/actionRecognition'

const state = {
  actionTableData: [],
  actionDataCount: +'',
  cateDataCount: +'',
  cateTableData: [],
  showDeleteBox: false,
  // 上传弹框内得项目列表
  uploadItemList: [],
  // 是否展示编辑盒子
  showEditBox: false,
  itemList: [],
  // 是否展示添加类目盒子
  showAddItem: false,
  // 是否展示删除类目盒子
  showDeleteItemBox: false,
  cateDataList: [],
  cateListCount: +'',
  dataList: [], // 数据模型表格数据
  dataCount: +''
}

const mutations = {
  // 行为 下拉框
  selectItem_list (state, cateList) {
    state.itemList = cateList
  },
  update_actionTableData (state, actionTableData) {
    state.actionTableData = actionTableData
  },
  update_actionDataCount (state, count) {
    state.actionDataCount = count
  },
  update_cateTableData (state, data) {
    state.cateTableData = data
  },
  update_cateDataCount (state, count) {
    state.cateDataCount = count
  }
}

const actions = {
  // 获取类目选项
  getCateList (context) {
    const projectId = localStorage.getItem('projectId')
    return new Promise((resolve, reject) => {
      getAreaList(projectId).then(res => {
        if (res.data.code === (200 || '200')) {
          context.commit('selectItem_list', res.data.data.data)
        }
        resolve(res.data.data)
      })
        .catch(err => {
          reject(err)
        })
    })
  },
  getActionTableData (context, payload) {
    return new Promise((resolve, reject) => {
      getActionList(payload).then(res => {
        if (res.data.code === (200 || '200')) {
          context.commit('update_actionTableData', res.data.data.data)
          context.commit('update_actionDataCount', res.data.data.count)
        }
        resolve(res)
      })
        .catch(err => {
          reject(err)
        })
    })
  },
  createDeviceNum (context) {
    const projectId = localStorage.getItem('projectId')
    return new Promise((resolve, reject) => {
      createNum(projectId).then(res => {
        if (res.data.code === (200 || '200')) {
          resolve(res.data)
        }
      })
        .catch(err => {
          reject(err)
        })
    })
  },
  getAccidentData (context) {
    return new Promise((resolve, reject) => {
      getAccidentList().then(res => {
        if (res.data.code === (200 || '200')) {
          resolve(res.data)
        }
      })
        .catch(err => {
          reject(err)
        })
    })
  },

  addCamera (context, payload) {
    payload.projectId = localStorage.getItem('projectId')
    return new Promise((resolve, reject) => {
      addDevice(payload).then(res => {
        if (res.data.code === (200 || '200')) {
        }
        resolve(res)
      })
        .catch(err => {
          reject(err)
        })
    })
  },
  getCurrentCamera (context, payload) {
    return new Promise((resolve, reject) => {
      getCameraInfo(payload).then(res => {
        if (res.data.code === (200 || '200')) {
        }
        resolve(res)
      })
        .catch(err => {
          reject(err)
        })
    })
  },
  removeAction (context, payload) {
    return new Promise((resolve, reject) => {
      deleteCamera(payload).then(res => {
        if (res.data.code === (200 || '200')) {
        }
        resolve(res)
      })
        .catch(err => {
          reject(err)
        })
    })
  },
  updateCamera (context, payload) {
    payload.projectId = localStorage.getItem('projectId')
    return new Promise((resolve, reject) => {
      updateDevice(payload).then(res => {
        if (res.data.code === (200 || '200')) {
        }
        resolve(res)
      })
        .catch(err => {
          reject(err)
        })
    })
  },
  // 区域管理
  getAreaCateList (context, payload) {
    payload.projectId = localStorage.getItem('projectId')
    return new Promise((resolve, reject) => {
      getCameraAreaList(payload).then(res => {
        if (res.data.code === (200 || '200')) {
          context.commit('update_cateTableData', res.data.data.data)
          context.commit('update_cateDataCount', res.data.data.count)
        }
        resolve(res)
      })
        .catch(err => {
          reject(err)
        })
    })
  },
  addActionArea (context, payload) {
    payload.projectId = localStorage.getItem('projectId')
    return new Promise((resolve, reject) => {
      addArea(payload).then(res => {
        if (res.data.code === (200 || '200')) {
        }
        resolve(res)
      })
        .catch(err => {
          reject(err)
        })
    })
  },
  updateActionArea (context, payload) {
    payload.projectId = localStorage.getItem('projectId')
    return new Promise((resolve, reject) => {
      updateArea(payload).then(res => {
        if (res.data.code === (200 || '200')) {
        }
        resolve(res)
      })
        .catch(err => {
          reject(err)
        })
    })
  },
  deleteActionArea (context, payload) {
    return new Promise((resolve, reject) => {
      deleteArea(payload).then(res => {
        if (res.data.code === (200 || '200')) {
        }
        resolve(res)
      })
        .catch(err => {
          reject(err)
        })
    })
  }
}
export default {
  namespaced: true,
  actions,
  state,
  mutations
}
